package org.ala.shopping_test.shopping_center.repository.shard_key;

import org.ala.tiktools.sharddb.mybatis.router.shard_key_router.IShardKey;
import org.ala.tiktools.tools.TimestampTools;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 买家卖家订单分片建
 * 1）根据买家卖家id + 时间分库（历史总天数）
 *      只能均衡数据，无法解决当天的热库（如果有大店大促爆单，就会形成热库）
 *      解决方法：对大客户单独配库
 * 2）根据时间序分表
 *      订单周期一过自然形成历史表
 *
 * @author ala
 * @date 2025-03-21 12:17
 */
public class ShoppingOrderBuyerSellerShardKey implements IShardKey {
    /**
     *  id
     */
    protected Long id;
    /**
     *  下单时间戳
     */
    protected long timestamp;
    /**
     *  下单时间戳范围闭区间
     */
    protected long timestampGte = -1, timestampLte = -1;

    public static ShoppingOrderBuyerSellerShardKey build() {return new ShoppingOrderBuyerSellerShardKey();}

    public String toString() {
        StringBuilder sbuf = new StringBuilder();
        sbuf.append("{type:ShoppingOrderBuyerSellerShardKey");
        if (id != null) {sbuf.append(String.format(", id:%d", id));}
        if (timestamp >= 0) {sbuf.append(String.format(", timestamp:%d", timestamp));}
        if (timestampGte >= 0) {sbuf.append(String.format(", timestampGte:%d", timestampGte));}
        if (timestampLte >= 0) {sbuf.append(String.format(", timestampLte:%d", timestampLte));}
        return sbuf.append("}").toString();
    }

    public ShoppingOrderBuyerSellerShardKey copy() {
        return ShoppingOrderBuyerSellerShardKey.build()
                .id(getId())
                .timestampGte(getTimestampGte()).timestampLte(getTimestampLte())
                ;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public ShoppingOrderBuyerSellerShardKey id(Long id) {
        setId(id);
        return this;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public ShoppingOrderBuyerSellerShardKey timestamp(long timestamp) {
        setTimestamp(timestamp);
        return this;
    }

    public long getTimestampGte() {
        return timestampGte;
    }

    public void setTimestampGte(long timestampGte) {
        if (timestampGte < 0) {return;}
        this.timestampGte = timestampGte;
        if (timestampLte < 0) {timestampLte = timestampGte + 7 * TimestampTools.DAY_MS;}
    }

    public ShoppingOrderBuyerSellerShardKey timestampGte(long timestampGte) {
        setTimestampGte(timestampGte);
        return this;
    }

    public Long getTimestampLte() {
        return timestampLte;
    }

    public void setTimestampLte(long timestampLte) {
        if (timestampLte < 0) {return;}
        this.timestampLte = timestampLte;
        if (timestampGte < 0) {timestampGte = timestampLte - 7 * TimestampTools.DAY_MS;}
    }

    public ShoppingOrderBuyerSellerShardKey timestampLte(long timestampLte) {
        setTimestampLte(timestampLte);
        return this;
    }
}
